package com.epfresh.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.bean.GoodsParameters;
import com.epfresh.bean.HomeCommTag;
import com.epfresh.bean.ProductCondition;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wzh on 2016/5/6.
 */
public class ProductConditionAdapter extends BaseAdapter implements View.OnClickListener {

    public Context context;


    private List<ProductCondition> list;
    private AdapterView.OnItemClickListener onItemClickListener;

    private GoodsParameters parameters;
    private int currentIndex = -1;
    //type == 1,时间排序,type == 2,筛选
    private int type = -1;

    public ProductConditionAdapter(Context context, List<ProductCondition> list) {
        this.context = context;
        this.list = list;
    }

    public void setList(List<ProductCondition> list) {
        if (this.list != null) {
            this.list.clear();
            if (list != null)
                this.list.addAll(list);
        } else {
            this.list = list;
        }
    }

    public int getCurrentIndex() {
        return currentIndex;
    }

    public void setCurrentIndex(int currentIndex) {
        this.currentIndex = currentIndex;
    }

    public GoodsParameters getParameters() {
        return parameters;
    }

    public void setParameters(GoodsParameters parameters) {
        this.parameters = parameters;
    }

    public void initParameters(GoodsParameters parameters, int type) {
        this.parameters = parameters;

        this.type = type;
        if (type == 1) {//排序
            String sortProperty = parameters.getSortProperty();

            for (int i = 0; i < list.size(); i++) {
                ProductCondition pc = list.get(i);
                String code = pc.getCode();
                String name = pc.getCode();
                Log.d("code", sortProperty + "--" + code);
                if (code != null && !"".equals(code) && code.equals(sortProperty)) {
                    currentIndex = i;
                    return;
                } else if (code != null && "all".equals(code)) {
                    pc.setCode(null);
                    currentIndex = -1;
                    return;
                } else {
                    currentIndex = 0;
                }

            }
        } else if (type == 2) {//筛选
            for (int i = 0; i < list.size(); i++) {
                String typeSingle = parameters.getFilterCode();

                ProductCondition pc = list.get(i);
                String code = pc.getCode();


                if (code != null && !"".equals(code) && code.equals(typeSingle)) {
                    currentIndex = i;
                    return;
                }

//                else if (type1 != 2 && code != null && !"".equals(code) && code.equals(typeSingle)){
//                    currentIndex = i;
//                }
                else {//什么也没选中
                    currentIndex = -1;
                }

            }
        }


    }

    public AdapterView.OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_more_market, null, false);
            vh.tvContent = (TextView) convertView.findViewById(R.id.tv_market);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        ProductCondition pc = list.get(position);
        vh.tvContent.setText(pc.getName());

        if (currentIndex == position) {
            vh.tvContent.setTextColor(context.getResources().getColor(R.color.green));
            vh.tvContent.setEnabled(false);
        } else {
            vh.tvContent.setTextColor(Color.parseColor("#626262"));
            vh.tvContent.setEnabled(true);

        }

        convertView.setTag(R.id.item_key_position, position);
        convertView.setOnClickListener(this);
        return convertView;
    }


    @Override
    public void onClick(View v) {
        Object o = v.getTag(R.id.item_key_position);
        if (o != null && o instanceof Integer) {
            if (onItemClickListener != null) {
                int position = (int) o;
                if (position < list.size()) {
                    ProductCondition pc = list.get(position);

                    if (pc != null) {
                        int type1 = pc.getType();
                        if (type1 == 1) {//排序
                            //时间
                            if ("all".equals(pc.getCode())) {
                                parameters.setSortProperty(null);

                            } else
                                parameters.setSortProperty(pc.getCode());
                            parameters.setSortName(pc.getName());
                        } else if (type1 == 2) {//筛选
                            parameters.setFilterCode(pc.getCode());
                            if ("0".equals(pc.getCode())) {
                                parameters.setTypes(null, null);
                                parameters.setTagId(null);
                            } else {
                                parameters.setTypes(pc.getCode(), null);
                                parameters.setTagId(null);
                            }
                            parameters.setFilterName(pc.getName());
                        } else {//默认
                            parameters.setFilterCode(pc.getCode());
                            parameters.setTypes(null, null);
                            parameters.setFilterName(pc.getName());
                            parameters.setTagId(pc.getCode());
                        }
                        parameters.setType(pc.getCode() + "");
                    }

                }
                onItemClickListener.onItemClick(null, v, position, position);
            }
        }
    }

    public class ViewHolder {
        TextView tvContent;
    }

    public List<ProductCondition> getSortCondition(boolean isFirst) {
        List<ProductCondition> pcs = new ArrayList<>();
        ProductCondition pc = new ProductCondition();
        pc.setName("综合排序");
        pcs.add(pc);
        if (isFirst)
            pc.setCode("all");
        pc = new ProductCondition();
        pc.setName("成交量最高");
        pc.setCode("salesQuantity");
        pcs.add(pc);
//        pc = new ProductCondition();
//        pc.setName("进场时间最新");
//        pc.setCode("pluckingTime");
//        pcs.add(pc);
        pc = new ProductCondition();
        pc.setName("更新时间最新");
        pc.setCode("modifiedTime");
        pcs.add(pc);
        return pcs;

    }

    public List<ProductCondition> getFilterCondition(boolean isType, List<HomeCommTag> tags) {

        List<ProductCondition> pcs = new ArrayList<>();
        ProductCondition pc = null;
        if (isType) {
            pc = new ProductCondition();
            pc.setName("全部商品");
            pc.setCode("0");
            pcs.add(pc);
            pc.setType(2);
            pc = new ProductCondition();
            pc.setName("现货商品");
            pc.setCode("1");
            pc.setType(2);
            pcs.add(pc);

            pc = new ProductCondition();
            pc.setName("预售商品");
            pc.setCode("2");
            pc.setType(2);
            pcs.add(pc);
        }
        if (tags != null) {
            for (int i = 0; i < tags.size(); i++) {
                HomeCommTag tag = tags.get(i);
                pc = new ProductCondition();
                pc.setName(tag.getName());
                pc.setCode(tag.getId());
                pc.setType(3);
                pcs.add(pc);
            }
        }
        return pcs;
    }


}
